package mysql

import (
	"short-video/short-video-rpc/proto/video"
	"time"
)

type VideoWorks struct {
	Id             uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	CreatedAt      time.Time `gorm:"column:created_at;type:datetime(3);default:NULL;" json:"created_at"`
	UpdatedAt      time.Time `gorm:"column:updated_at;type:datetime(3);default:NULL;" json:"updated_at"`
	DeletedAt      time.Time `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
	Title          string    `gorm:"column:title;type:varchar(100);comment:标题;default:NULL;" json:"title"`                        // 标题
	Desc           string    `gorm:"column:desc;type:varchar(255);comment:描述;default:NULL;" json:"desc"`                          // 描述
	MusicId        int32     `gorm:"column:music_id;type:int;comment:选择音乐;default:NULL;" json:"music_id"`                       // 选择音乐
	WorkType       string    `gorm:"column:work_type;type:varchar(20);comment:作品类型;default:NULL;" json:"work_type"`             // 作品类型
	CheckStatus    string    `gorm:"column:check_status;type:varchar(10);comment:审核状态;default:NULL;" json:"check_status"`       // 审核状态
	IpAddress      string    `gorm:"column:ip_address;type:varchar(20);comment:IP地址;default:NULL;" json:"ip_address"`             // IP地址
	WorkPermission string    `gorm:"column:work_permission;type:varchar(20);comment:作品权限;default:NULL;" json:"work_permission"` // 作品权限
	LikeCount      int32     `gorm:"column:like_count;type:int;comment:喜欢数量;default:NULL;" json:"like_count"`                   // 喜欢数量
	CommentCount   int32     `gorm:"column:comment_count;type:int;comment:评论数;default:NULL;" json:"comment_count"`               // 评论数
}

func (w *VideoWorks) TableName() string {
	return "video_works"
}

func (w *VideoWorks) CreateMusic(in *video.VideoUploadReq) error {
	works := &VideoWorks{MusicId: int32(in.MusicId)}
	return DB.Debug().Where("id =?", in.Id).Create(&works).Error
}

func (w *VideoWorks) GetWorksById(id int64) error {
	return DB.Debug().Where("id=?", id).First(&w).Error
}

func (w *VideoWorks) GetUpdatyeBtTitle(title string) error {
	return DB.Debug().Table("video_works").Update("title", title).Error
}

func (w *VideoWorks) CreateVideo(in *video.VideoCreateReq) error {
	works := &VideoWorks{
		Title:       in.Title,
		Desc:        in.Desc,
		MusicId:     int32(in.MusicId),
		WorkType:    in.WorkType,
		CheckStatus: in.CheckStatus,
	}
	return DB.Debug().Create(&works).Error
}

func (w *VideoWorks) GetWorksByCheck(status string) (list []*VideoWorks, err error) {
	err = DB.Debug().Where("check_status=?", status).Find(&list).Error
	if err != nil {
		return nil, err
	}
	return
}

func (w *VideoWorks) UpdateLikes(id int64, count int32) error {
	return DB.Debug().Table("video_works").Where("id=?", id).Update("like_count", count+1).Error
}

func (w *VideoWorks) UpdateLikej(id int64, count int32) error {
	return DB.Debug().Table("video_works").Where("id=?", id).Update("like_count", count-1).Error
}
